Next | Prev | Up | Top | Contents | Index

Reader/Writer Locks

Reader/writer locks are similar to sleep locks in that they are designed for mutually exclusive control of resources for relatively long periods of time. However, Reader/Writer locks are optimized for the case in which the resource is often used by processes that only interrogate it (readers), and only rarely used by processes that modify it (writers).

Reader/writer locks compatible with SVR4 are introduced in IRIX 6.2. The functions are summarized in Table 9-19.

Functions for Reader/Writer Locks
Function NameHeader FilesCan Sleep?Purpose
RW_ALLOC(D3) types.h & kmem.h & ksynch.hYAllocate and initialize a reader/writer lock.
RW_DEALLOC(D3) types.h & ksynch.hNDeallocate a reader/writer lock.
RW_INIT(D3) types.h & ksynch.hNInitialize an existing reader/writer lock.
RW_DESTROY(D3) types.h & ksynch.hNDeinitialize an existing reader/writer lock.
RW_RDLOCK(D3) types.h & ksynch.h & param.hYAcquire a reader/writer lock as reader, waiting if necessary.
RW_TRYRDLOCK(D3) types.h & ksynch.hNTry to acquire a reader/writer lock as reader, returning a code if it is not free.
RW_TRYWRLOCK(D3) types.h & ksynch.hNTry to acquire a reader/writer lock as writer, returning a code if it is not free.
RW_UNLOCK(D3) types.h & ksynch.hNRelease a reader/writer lock as reader or writer.
RW_WRLOCK(D3) types.h & ksynch.h & param.hYAcquire a reader/writer lock as writer, waiting if necessary.

Although allocation and deallocation functions are supplied, a mrlock_t type is a small object that is normally allocated as a static variable or as a field of a structure. The RW_INIT() operation prepares a statically-allocated mrlock_t for use.

A process that intends to modify a resource uses RW_WRLOCK to claim it. This process waits until the resource is not in use by any process, then it gains exclusive access. Only one process is allowed to hold a reader/writer lock as a writer. All other processes, readers or writers, wait until the writer releases the lock.

A process that intends only to interrogate a resource uses RW_RDLOCK to gain access. If a writer holds the lock, the process waits. When the lock is free, or is held only by other readers, the process continues. More than one reader can hold a reader/writer lock at one time. It is also valid for a reader to "double-trip" a reader/writer lock; that is, claim it two or more times. The reader must release the lock as many times as it claimed the lock.

A reader/writer lock serves the same basic purpose as a sleep lock, but it is more efficient in a multiprocessor when there are frequent, read-only uses of a resource.


Next | Prev | Up | Top | Contents | Index